package com.example.NearByLocation;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */



    List<String> firstList = new ArrayList<String>();
    private ListView firstlist_listview;
    private Button classify_Button;
    private TextView classify_TextView;
    private Button first_location_bt;
    private TextView first_location_textview;
    private  LocationManager locationManager ;
    private Button first_search_bt;
    private Button first_setting_bt;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
//显示列表
        DataList dataList=new DataList();
        firstList= dataList.getSearchItemData();
        FirstAdapter firstAdapter=new FirstAdapter(firstList,R.layout.list_item,this);
        firstlist_listview=(ListView) findViewById(R.id.firstlist_listview);
        firstlist_listview.setAdapter(firstAdapter);
//取控件
        first_location_bt = (Button) findViewById(R.id.first_location_bt);
        first_location_textview = (TextView) findViewById(R.id.first_location_textview);
        locationManager = new LocationManager(MyActivity.this,first_location_textview);
        first_search_bt = (Button) findViewById(R.id.first_search_bt);
        first_setting_bt=(Button)findViewById(R.id.first_setting_bt);
//点击事件
        first_location_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                locationManager.getCurretLocation(MyActivity.this);
            }
        });

        first_search_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyActivity.this,SearchBtActivity.class);
                startActivity(intent);

            }
        });
        first_setting_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MyActivity.this,SetActivity.class);
                startActivity(intent);
            }
        });
    }

    class FirstAdapter extends ListAdapter {
        FirstAdapter(List<String> list, int resource, Context context) {
            super(list, resource, context);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            classify_TextView = (TextView) view.findViewById(R.id.classify_TextView);

            final String serviceName = classify_TextView.getText().toString();
            classify_Button = (Button) view.findViewById(R.id.classify_Button);
            classify_Button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    firstJump(serviceName);
                }
            });
            classify_TextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(MyActivity.this,PoiListActivity.class);
                    SharedPreferences.Editor editor=getSharedPreferences("ServiceName",MODE_PRIVATE).edit();
                    editor.putString("thirdName",serviceName);
                    editor.commit();
                    startActivity(intent);
                }
            });
            return view;
        }

        private void firstJump(String serviceName) {
            SharedPreferences.Editor editor=getSharedPreferences("ServiceName",MODE_PRIVATE).edit();
            editor.putString("firstName",serviceName);
            editor.commit();
            Intent intent = new Intent(MyActivity.this, SecondActivity.class);
            startActivity(intent);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        locationManager.stopLocation();
    }
}
